﻿@Html.HeadTitle("Publishers")
@Html.Partial("_TopNav")

<div id="tabContent">
    <table id="publisherTable"></table>
</div>

@Html.Partial("_PublisherDialog")

<script type="text/javascript">
    function AddPublisher() {
        PublisherDialog.open({
            PublisherId: null,
            PublisherName: "",
            City: ""
        },
        function (wasSaved) {
            if (wasSaved)
                $('#publisherTable').flexReload();
        });
    }

    function DeletePublisher() {
        var publisher = GetSelectedPublisher();
        if (publisher === null) {
            alert("No publisher is selected.");
            return;
        }
        
        if (! confirm("Do you really want to delete " + publisher.PublisherName + "?"))
            return;

        $.post('@Url.Action("PublisherDelete")', { id: publisher.PublisherId }, function(data) {
            $('#publisherTable').flexReload();
        });
    }

    function EditPublisher() {
        var publisher = GetSelectedPublisher();
        if (publisher === null) {
            alert("No publisher is selected.");
            return;
        }

        PublisherDialog.open(publisher, function (wasSaved) {
            if (wasSaved)
                $('#publisherTable').flexReload();
        });
    }

    function GetSelectedPublisher() {
        var selection = $('#publisherTable').flexGetSelection();
        if (selection.total === 0)
            return null;

        return {
            PublisherId: selection.rows[0].id,
            PublisherName: selection.rows[0].cell["PublisherName"],
            City: selection.rows[0].cell["City"]
        }
    }

    $(function () {
        $('#publisherTable').flexigrid({
            url: '@Url.Action("PublisherSearch", "Publisher")',
            dataType: 'json',
            onSuccess: function () {
                $('#publisherTable').flexTemplate({
                    colIndex: 0,
                    template: '<a href=\'@Url.Action("PublisherDetail")/{{id}}\'>{{text}}</a>'
                });
            },

            singleSelect: true,
            buttons: [
                { name: 'Edit', bclass: 'edit', onpress: EditPublisher },
                { name: 'Add', bclass: 'plus', onpress: AddPublisher },
                { name: 'Delete', bclass: 'minus', onpress: DeletePublisher }
            ],

            colModel: [
                { display: 'Publisher Name', name: 'PublisherName', width: 200, sortable: true },
                { display: 'City', name: 'City', width: 100, sortable: true },
                { display: 'Number of Books', name: 'BookCount', width: 100, sortable: false },
                { display: 'Number of Authors', name: 'AuthorCount', width: 100, sortable: false }
            ],

            sortname: 'PublisherName',
            sortorder: 'asc',
            usepager: true,
            rp: 20,
            useRp: false,
            showToggleBtn: false,
            nomsg: 'There are no Publishers in the database'
        });
    });
</script>